const { Client } = require('pg');
require('dotenv').config({ path: '../backend/.env' });

// 数据库连接配置
const client = new Client({
  host: process.env.DB_HOST || 'localhost',
  port: process.env.DB_PORT || 5432,
  database: process.env.DB_NAME || 'financeapp',
  user: process.env.DB_USER || 'financeapp_admin',
  password: process.env.DB_PASSWORD || 'financeapp_password',
});

async function queryUsers() {
  try {
    // 连接到数据库
    await client.connect();
    console.log('Connected to the database successfully');
    
    // 查询所有用户
    const result = await client.query('SELECT id, username, phone, password_hash FROM users');
    
    console.log('\nUsers in the database:');
    console.log('=====================================');
    result.rows.forEach(user => {
      console.log(`ID: ${user.id}`);
      console.log(`Username: ${user.username}`);
      console.log(`Phone: ${user.phone}`);
      console.log(`Password Hash: ${user.password_hash}`);
      console.log('-------------------------------------');
    });
    
    await client.end();
  } catch (error) {
    console.error('Error querying users:', error.message);
    if (client) {
      await client.end();
    }
  }
}

queryUsers();